﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：静态页面管理Ajax请求服务端处理程序。
  //创建人：赵国伟
  //生成时间：2012/03/24 14:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Shopping.Common;
using Shopping.Model;
using Shopping.BLL;

public partial class Manage_AS_PagesCommon : BaseManagePage
{
    private readonly PagesManage bllPages = new PagesManage();
    private ReturnValue ret = new ReturnValue(ResultType.Fail); 

    protected void Page_Load(object sender, EventArgs e)
    {
         string action = Request.QueryString["Action"] as string;
         switch (action)
         {
             case "SavePage":
                 SavePage();
                 break;
             case "SaveContent":
                 SaveContent();
                 break;
             case "GetPage": //查询一条指定的页面信息(含内容)； by 赵国伟 2012-04-15
                 GetPage();
                 break;
             case "GetPageAndParam": //查询一条页面信息(不含内容)；by 赵国伟 2012-04-15
                 GetPageAndParam();
                 break;
             case "DeletePage": //删除指定的一条页面信息； by 赵国伟 2012-04-15;
                 DeletePage();
                 break;
         }
         Response.End();
    }


    #region "保存页面信息"
    /// <summary>
    /// 保存页面信息；
    /// </summary>
    private void SavePage()
    {
        ret = BLLHelper.Save<PagesManage, PageInfo>("SavePage");
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "保存页面内容信息；"
    /// <summary>
    /// 保存页面内容信息；
    /// </summary>
    private void SaveContent()
    {
        string jsonData = Request["jsonData"] + "";  //获取传递过来的Json对象数据；
        List<PageContents> listOfPageContents;
        ReturnValue ret = new ReturnValue();
        try
        {
            ///将提交上来的Json格式的数据装化为相应的实体类；
            listOfPageContents = JSONHelper.JsonStringToObject<List<PageContents>>(jsonData);
           

        }
        catch (Exception e)
        {
            ret.Message = "数据提取失败！" + e.Message;
            Response.Write(ret.ToJsonString());
            return;
        }


        for (int i = 0; i < listOfPageContents.Count; ++i)
        {
            listOfPageContents[i].ParamContent = Utility.P("Content"+(i+1));
        }

        ret = bllPages.SaveContent(listOfPageContents);
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "查询一条指定的页面信息； by 赵国伟 2012-04-15"
    /// <summary>
    /// 查询一条指定的页面信息
    /// </summary>
    private void GetPageAndParam()
    {
        int PageID = Utility.G<int>("PageID");
        PageInfo p = bllPages.GetPage(PageID,false);
        if (p != null)
            Response.Write(JSONHelper.ObjectToJson<PageInfo>(p));
    }
    #endregion

    #region "查询一条指定的页面信息(含内容) by 赵国伟 2012-04-15 "
    /// <summary>
    /// 查询一条指定的页面信息(含内容)
    /// </summary>
    private void GetPage()
    {
        int PageID = Utility.G<int>("PageID");
        PageInfo p = bllPages.GetPage(PageID, true);
        if (p != null)
            Response.Write(JSONHelper.ObjectToJson<PageInfo>(p));
    }
    #endregion

    #region "删除一条指定的页面信息； by 赵国伟 2012-04-15"
    /// <summary>
    /// 删除一条指定的页面信息；
    /// </summary>
    private void DeletePage()
    {
        int PageID = Utility.G<int>("PageID");
        if (bllPages.DeletePage(PageID) > 0)
        {
            ret.Result = ResultType.Success;
            ret.Message = "删除成功！";
        }
        else
        {
            ret.Message = "删除失败！";
        }
        Response.Write(ret.ToJsonString());
    }
    #endregion



}